package com.zking.controller;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zking.model.Answerquestions;
import com.zking.model.Collects;
import com.zking.model.Questions;
import com.zking.model.Users;
import com.zking.service.QuestionService;
import com.zking.util.AppException;
import com.zking.util.Result;

@Controller
@RequestMapping(value="/ques")
public class QuestionController {
	@Autowired
	@Qualifier("questionService")
	private QuestionService questionService;
	
	
	//退出登陆
		@RequestMapping("/exit")
	public @ResponseBody String exit(HttpServletRequest request,HttpServletResponse response){
		String str = "";
		HttpSession session = request.getSession();
		//清除session中存储的用户数据
		session.setAttribute("user",null);
		str = "0";
		return "str";
	}
		
		
		
	//提问
	@RequestMapping(value="/ask",produces = "application/json;charset=utf-8")
	public @ResponseBody String askques(HttpServletRequest req,String qtitle,String qbody){
		String str="";
		Users user=(Users) req.getSession().getAttribute("user");
		int uid=user.getuId();
		if("".equals(qtitle.trim())){
			str = "2";
		}else if("".equals(qbody.trim())){
			str="3";
		}else if("".equals(qtitle.trim()) && "".equals(qbody.trim())){
			str="4";
		}else{
			Questions question = new Questions();
			question.setqTitle(qtitle);
			question.setqBody(qbody);
			question.setqApplytime(new Date());
			question.setqDostate(1);
			question.setuId(uid);
			try {
				questionService.tquestion(question);
				str="0";
			} catch (AppException e) {
				// TODO Auto-generated catch block
				str=e.toString();
			}
		}
		return str;
	}
	
	@RequestMapping(value="/askques")
	public String askques(){
		return "askquestion";
	}
	//显示所有的问题
	@RequestMapping(value="/index")
	public String showques(HttpServletRequest req,Model model){
		Users user = (Users) req.getSession().getAttribute("user");
		List<Questions> qlist=null;
		List<Questions> nolist = null;
		List<Questions> yeslist = null;
		try {
			qlist=questionService.showquestion();
			nolist = questionService.selectbydostate(1);
			yeslist = questionService.selectbydostate(2);
			if(qlist.size()>0){
				model.addAttribute("questions", qlist);
				model.addAttribute("pages", qlist.get(0));
			}
			if(nolist.size()>0){
				model.addAttribute("noanswers",nolist);
				model.addAttribute("pageno", nolist.get(0));
			}
			if(yeslist.size()>0){
				model.addAttribute("yesanswers",yeslist);
				model.addAttribute("pageyes", yeslist.get(0));
			}
		} catch (AppException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "index";
	}
	
	//查看问题的详细信息
	@RequestMapping(value="/answerques/{qId}")
	public String answerques(HttpServletRequest req ,Model model,@PathVariable Integer qId){
		HttpSession session = req.getSession();
		List<Answerquestions> alist=null;
		try {
			alist=questionService.selectByqid(qId);
			if(alist.size()>=0){
				model.addAttribute("answerquestion", alist);
			}
			//获得问题数据并存入model中
			session.setAttribute("question", questionService.selectQuestionsByqid(qId));
		} catch (AppException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "answerquestion";
	}
	
	//回答问题
	@RequestMapping(value="/answer")
	public @ResponseBody String answer(HttpServletRequest req,String qbody){
		String str="";
		Users user = (Users) req.getSession().getAttribute("user");
		qbody.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;");
		if(null!=user){
			int uid = user.getuId();
			Questions question = (Questions) req.getSession().getAttribute("question");
			int qid = question.getqId();
			System.out.println("qbody:"+qbody);
			if("".equals(qbody.trim())){
				str = "2";
			}else{
				Answerquestions answerquestion = new Answerquestions();
				answerquestion.setaBody(qbody);
				answerquestion.setaApplytime(new Date());
				answerquestion.setqId(qid);
				answerquestion.setuId(uid);
				answerquestion.setaDostate(1);
				try {
					questionService.answerquestion(answerquestion);
					str="0";
				} catch (AppException e) {
					// TODO Auto-generated catch block
					str=e.toString();
				}
			}
		}else{
			str = "1";
		}
		return str;
	}
	//验证用户是否已经收藏了这个问题
	@RequestMapping(value="/collectques/{qId}")
	public @ResponseBody String collectques(HttpServletRequest req,@PathVariable Integer qId,Model model){
		String str = "";
		Users user = (Users) req.getSession().getAttribute("user");
		if(user!=null){
			int uId = user.getuId();
			Collects collect = new Collects();
			collect.setqId(qId);
			collect.setuId(uId);
			try {
				int c = questionService.collects(collect);
				model.addAttribute("collects", collect);
				if(c==0){
					str = "1";
				}else{
					str = "0";
				}
			} catch (AppException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else{
			str = "2";
		}
		return str;
	}
	//根据cId删除用户收藏的问题
	@RequestMapping(value="/deletecollect",produces = "application/json;charset=utf-8")
	public @ResponseBody String deletecollect(int cId){
		String str = "";
		try {
			questionService.deletecollect(cId);
			str = "0";
		} catch (AppException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return str;
	}
	//根据qId删除用户提过的问题
	@RequestMapping(value="/deletequestion",produces = "application/json;charset=utf-8")
	public @ResponseBody String deletequestion(int qId){
		String str = "";
		System.out.println(qId);
		try {
			questionService.deletequestion(qId);
			str = "0";
		} catch (AppException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return str;
	}
	//采纳回答的答案改变状态
	@RequestMapping(value="/cnanswer",produces = "application/json;charset=utf-8")
	public @ResponseBody String cnanswer(int aId,Model model,int qId,HttpServletRequest req,int uId){
		String str = "";
		HttpSession session = req.getSession();
		Questions question = new Questions();
		question.setqDostate(2);
		Answerquestions answerquestion = new Answerquestions();
		answerquestion.setaDostate(2);
		List<Answerquestions> alist=null;
		try {
			alist=questionService.selectByqid(qId);
			model.addAttribute("answerquestion", alist);
			questionService.updateanswer(qId, question.getqDostate());
			questionService.updateanswerdostate(aId,answerquestion.getaDostate());
			str = "0";
		} catch (AppException e) {
			e.printStackTrace();
		}
		return str;
	}
	//根据条件查找问题
	@RequestMapping(value="/search",produces = "application/json;charset=utf-8")
	public @ResponseBody String search(String qTitle,Model model,HttpServletRequest req){
		String s = "";
		List<Questions> qlist = null;
		HttpSession session = req.getSession();
		try {
			if("".equals(qTitle)){
			
			}else{
				qlist = questionService.selectByTJ(qTitle);
				session.setAttribute("searchs", qlist);
				System.out.println(qlist.size());
				for(int i=0;i<qlist.size();i++){
					System.out.println("qTitle:"+qlist.get(i).getqTitle());
				}
					s = "0";
			}
		} catch (AppException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return s;
	}
	//跳到搜索页面
	@RequestMapping(value="/searchs")
	public String ss(){
		return "search";
	}
}
